package com.buba.customer.controller;

import com.buba.customer.pojo.CustomerLogin;
import com.buba.customer.pojo.CustomerLoginLog;
import com.buba.customer.service.CustomerLoginLogService;
import com.buba.customer.service.CustomerLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户登录的控制层
 */
@Controller
@RequestMapping("/CustomerLogin")
public class CustomerLoginController {
    private CustomerLoginService customerLoginService;
    private CustomerLoginLogService customerLoginLogService;
    private DateFormat dateFormat;


    /**
     * 修改用户信息
     */
    @RequestMapping("/updCustomerLogin")
    @ResponseBody
    public Map<String,Object> updCustomerLogin(CustomerLogin customerLogin){

        Integer count = customerLoginService.updCustomerLogin(customerLogin);
        Map<String,Object> map = new HashMap<>();
        if(count ==1){
            map.put("msg","修改完成");
        }else{
            map.put("msg","修改失败");
        }

        return map;
    }

    /**
     * 删除用户信息
     * @return
     */
    @RequestMapping("/delCustomerLogin")
    @ResponseBody
    public Map<String,Object> delCustomerLogin(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");

        Integer count = customerLoginService.delCustomerLogin(split);
        map.put("msg","删除了"+count+"条数据");
        return map;
    }
    /**
     * 查询用户的登录信息
     * layui表格要求以json格式返回
     */
    @RequestMapping("/selCustomerLogin")
    @ResponseBody
    public Map<String,Object> selCustomerLogin(Integer page,Integer limit,String startTime,String endTime){
        Map<String,Object> map = new HashMap<>();
        map.put("startTime",startTime);
        map.put("endTime",endTime);

        //查询总条数，查询当前页的数据
        Long count = customerLoginService.selCount(map);
        //把分页参数携带过去
        map.put("page",(page-1) * limit);
        map.put("limit",limit);


        List<CustomerLogin> list = customerLoginService.selCustomerLogin(map);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);

        return map;
    }
    /**
     * 添加用户的登录信息
     * 表单中传过来的参数 直接写到方法的参数中
      *ResponseBody 使用ajax返回时传递json数据
     */
    @RequestMapping("/addCustomerLogin")
    @ResponseBody
    public Map<String,Object> addCustomerLogin(CustomerLogin login){
        Map<String,Object> map = new HashMap<>();
        //把调用业务层查询账号是否被注册
        Integer exists = customerLoginService.selCustomerLoginByName(login);
        if(exists > 0){
            map.put("msg","该账号已经被注册");
            return map;
        }

        //把要添加的信息传入业务层
        Integer count = customerLoginService.addCustomerLogin(login);

        if(count==1) {
            map.put("msg", "添加了" + count + "条信息");
        }
        return map;
    }
    /**
     * 用户登录方法
     *
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, CustomerLogin login) {
        // 调用业务层登录方法，把帐号密码传入，返回用户的登录信息
        CustomerLogin customerLogin = customerLoginService.login(login);
        // 判断是否存在该用户
        if (customerLogin != null) {
            //登录成功把登录的用户保存到Session中
            session.setAttribute("customerLogin", customerLogin);
            session.removeAttribute("message");
            //添加登录日志
            addCustomerLoginLog(customerLogin.getCustomerId(), 1, request);
            // 默认转发，转发地址栏不会变化，会导致页面所有的相对路径错误
            // 所以改成重定向
            return "redirect:/pinxixi/index.jsp";
        }
        addCustomerLoginLog(null, 0, request);
        session.setAttribute("message", "帐号或密码错误");
        return "redirect:/login.jsp";
    }


    /**
     * 添加用户登录日志
     *
     * @param CustomerId 登录用户的id
     * @param loginType  登录类型 0、未成功 1、成功
     * @param request    获取用户ip需要用到的请求对象
     */
    private void addCustomerLoginLog(Integer CustomerId, int loginType, HttpServletRequest request) {
        CustomerLoginLog customerLoginLog = new CustomerLoginLog();
        // 当前登录到用户id
        customerLoginLog.setCustomer_id(CustomerId);
        // 用户登录的ip String remoteIp=request.getRemoteAddr();
        customerLoginLog.setLogin_ip(request.getRemoteAddr());
        // 登录时间
        customerLoginLog.setLogin_time(dateFormat.format(new Date()));
        // 登录类型
        customerLoginLog.setLogin_type(loginType);
        // 调用登录日志业务层
        customerLoginLogService.addCustomerLoginLog(customerLoginLog);
    }



    @Autowired
    public void setCustomerLoginService(CustomerLoginService customerLoginService) {
        this.customerLoginService = customerLoginService;
    }
    public CustomerLoginService getCustomerLoginService() {
        return customerLoginService;
    }

    public CustomerLoginLogService getCustomerLoginLogService() {
        return customerLoginLogService;
    }
    @Autowired
    public void setCustomerLoginLogService(CustomerLoginLogService customerLoginLogService) {
        this.customerLoginLogService = customerLoginLogService;
    }

    public DateFormat getDateFormat() {
        return dateFormat;
    }
    @Autowired
    public void setDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }
}
